<template>
  <el-dialog :title='title' :visible.sync="dialogFormVisible" @closed="$emit('update:activated', false)" width="800px">
    <el-card>
      <div class="dialogStyle" style="display: flex;flex-direction: column">
        <el-form :inline="true" class="search-form search-form-normal" ref="searchForm" size="mini">
          <!--<el-form-item :label="$t('pageTable.MatterMaterialSpecificNumber')">-->
          <!--<el-input :disabled="multipleSelectionT.length>1" v-model="form.materialCode" class="search-form-item-input" style="width:160px" size="mini"-->
          <!--:maxlength="32" clearable></el-input>-->
          <!--<div style="float: right;margin-right: 40px;"><el-button :disabled="form.materialCode==''" @click="exec_search()">{{$t('searchFrom.checkAndGet')}}</el-button></div>-->

          <!--</el-form-item>-->
          <el-form-item :label="$t('pageTable.contractNo')" prop="contractNo">
            <el-input :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:160px" v-model="form.contractNo"></el-input>
            <div style="float: right;margin-right: 40px;">
              <el-button :disabled="form.contractNo==''" @click="initInfoByContractNo(form.contractNo)">
                {{$t('searchFrom.checkAndGet')}}
              </el-button>
            </div>

          </el-form-item>
          <el-form-item :label="$t('pageTable.packingDate')" prop="packingDate">
            <el-date-picker
              style="width:160px"
              type="date"
              v-model="form.packingDate"
              value-format="yyyyMMddHHmmss">
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="$t('pageTable.departureDate')" prop="departureDate">
            <el-date-picker
              style="width:160px"
              type="date"
              v-model="form.departureDate"
              value-format="yyyyMMddHHmmss">
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="$t('pageTable.TypeOfShipping')" prop="shippingMethods">
            <el-select clearable size="mini" style="width: 160px" v-model="form.shippingMethods">
              <el-option value=""></el-option>
              <el-option
                :key="item.value"
                :label="item.name"
                :value="item.value"
                size="mini"
                v-for="item in transportations">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('pageTable.billNo')" prop="billNo">
            <el-input :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:160px" v-model="form.billNo"></el-input>
          </el-form-item>
          <el-form-item :label="$t('pageTable.containerSize')" prop="containerSize">
            <!--<el-input v-model="form.containerSize" class="search-form-item-input" style="width:160px" size="mini"-->
            <!--:maxlength="32" clearable></el-input>-->
            <el-radio-group class="" size="mini" style="width:250px" v-model="form.containerSize">
              <el-radio label="20GP">20GP</el-radio>
              <el-radio label="40GP">40GP</el-radio>
              <el-radio label="40HC">40HC</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="$t('pageTable.containerNo')" prop="containerNo">
            <el-input :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:160px" v-model="form.containerNo"></el-input>
          </el-form-item>
          <el-form-item :label="$t('pageTable.freightPremium')" prop="freightPremium">
            <el-input :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:160px" v-model="form.freightPremium"></el-input>
          </el-form-item>
          <el-form-item label="实际总数量">
            <el-input :disabled="true" :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:100px" v-model="realAmount"></el-input>
          </el-form-item>
          <el-form-item label="超发数量">
            <el-input :disabled="true" :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:100px" v-model="supperAmount"></el-input>
          </el-form-item>
          <el-form-item label="建议数量">
            <el-input :disabled="true" :maxlength="32" class="search-form-item-input" clearable size="mini"
                      style="width:100px" v-model="allSuggest"></el-input>
          </el-form-item>


        </el-form>
        <el-table
          :data="search_result"
          :height="600"
          border
          class="search-result-table"
          ref="multipleTable"
          size="mini"
          style="flex: 1"
          tooltip-effect="dark">
          <!--          <el-table-column prop="orderCode"  width="100" align="center" :label="成品编码"  />-->
          <!--          <el-table-column prop="orderAmount"  width="80" align="center" :label="订单数量"  />-->
          <!--<el-table-column prop="orderDate"  width="100" align="center" :label="$t('pageTable.orderDate')">-->
          <!--<template slot-scope="scope">-->
          <!--{{scope.row.orderDate != null ?$moment(scope.row.orderDate,'YYYYMMDDHHmmss').format('YYYY-MM-DD') : ''}}-->
          <!--</template>-->
          <!--</el-table-column>-->
          <el-table-column :label="$t('pageTable.OrderInfoOrderCode')" :show-overflow-tooltip="true" align="center"
                           prop="orderCode"/>
          <el-table-column :label="$t('pageTable.OrderInfoOrderDate')" :show-overflow-tooltip="true" align="center">
            <template slot-scope="scope">
              {{scope.row.orderDate != null ?$moment(scope.row.orderDate,'YYYYMMDD').format('YYYY-MM-DD') : ''}}
            </template>
          </el-table-column>
          <el-table-column :label="$t('pageTable.MatterMaterialSpecificNumber')" :show-overflow-tooltip="true"
                           align="center" prop="materialCode"/>
          <el-table-column :label="$t('pageTable.UnitConsumptionQuantity')" prop="materialAmount">
            <template slot-scope="scope">
              <el-input @blur="checkPrice(scope.row.amount)" v-model="scope.row.materialAmount"></el-input>
            </template>
          </el-table-column>
          <!--<el-table-column prop="supperAmount" :label="$t('pageTable.supperAmount')"/>-->
          <!--<el-table-column prop="suggestedAmount" :label="$t('pageTable.suggestedAmount')" ></el-table-column>-->
          <el-table-column :label="$t('pageTable.ConvertedQuantity')" :show-overflow-tooltip="true" align="center"
                           prop="materialRelationQuantity"/>

          <!--<el-table-column fixed="right" width="120" :label="操作" >-->
          <!--<template slot-scope="scope" >-->
          <!--<el-button title="收货" type="primary" size="mini" class="btn-opt" plain  @click="deleteMatter(scope.row.uuid)">-->
          <!--确认收货</el-button>-->
          <!--</template>-->
          <!--</el-table-column>-->
        </el-table>
      </div>
    </el-card>
    <div class="dialogButton">
      <el-button :disabled="confirm" @click="confirmDeliery()" size="mini" type="primary"><i
        class="fa fa-check"></i> {{$t('pageTable.ConfirmationOfDelivery')}}
      </el-button>
      <el-button @click="cancel " size="mini">{{$t('searchFrom.cancel')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import ui_config from '@/config/ui_config'
  import activityService from '@/api/warehouse/planDetail'
  import service from '@/api/warehouse/GoodsList'

  export default {
    name: "ReceiveGoods",
    props: {
      multipleSelectionT: {},
    },
    data() {
      return {
        dialogFormVisible: true,
        title: this.$t('pageTable.DeliveryDetails'),
        PAGE_SIZES: ui_config.PAGE_SIZES,
        idsStr: [],
        form: {
          materialCode: "",
          contractNo: "",
          packingDate: "",
          departureDate: "",
          shippingMethods: "",
          billNo: "",
          containerSize: "",
          containerNo: "",
          freightPremium: "",
        },
        shipNo: "",
        containerNo: "",
        contractNo: "",
        orderCode: "",
        orderDate: '',
        orderAmount: "",
        materialCode: "",
        materialAmount: "",
        materialRelationQuantity: "",
        supperAmount: "",
        suggestedAmount: "",
        search_result: [],
        allSuggest: "",
        transportation: "",
        billNo: "",
        transportations: [],
      }
    },
    async mounted() {
      await activityService.sendGoodsInit().then(resp => {
        this.transportations = resp.data.result.transportation
      });
      // if (this.multipleSelectionT.length==1){
      //   this.form.materialCode=this.multipleSelectionT[0]
      // }else if (this.multipleSelectionT.length==0){
      //   return
      // }
      this.exec_search();
    },
    computed: {
      confirm() {
        return this.search_result.some(item => {
          return item.amount == 0
        })
      },
      realAmount() {
        let a = 0;
        this.search_result.forEach(item => {
          a += parseInt(item.materialAmount)
        });
        return a;
      }
    },
    methods: {
      initInfoByContractNo(contractNo) {
        service.initInfoByContractNo({contractNo}).then(resp => {
          if (resp.data.code == 200) {
            this.form.billNo = resp.data.result.billNo;
            this.form.packingDate = resp.data.result.packingDate;
            this.form.departureDate = resp.data.result.departureDate;
            this.form.paymentDay = resp.data.result.paymentDay;
            this.form.paymentDate = resp.data.result.paymentDate;
          } else {
            this.$notify({message: resp.data.message, type: "error"});
          }
        }, error => {

        })
      },
      exec_search() {
        let idsStrT = [];
        // if (this.multipleSelectionT.length<2){
        //   idsStrT.push(this.form.materialCode)
        // } else {
        idsStrT = this.multipleSelectionT
        // }
        activityService.finddeliverGoodsList({idsStrT: idsStrT, dateFlag: this.dateFlag}).then(resp => {
          this.search_result = resp.data.resultList;                //视图展示查询结果
          this.supperAmount = this.search_result[0].supperAmountQut.supperAmount
          this.allSuggest = 0;
          this.search_result.forEach(item => {
            this.allSuggest += item.materialAmount
          })

        }, err => {
          console.error(err);
        })
      },
      checkPrice(amount) {
        // let pattern = /^([0-9]*)+\.{0,1}[0-9]{1,3}$/;
        // if(!pattern.test(amount)){
        //     this.$notify({message: this.$t('Tips.floatCheck'), type: "error"});
        // }
      },
      cancel() {
        this.dialogFormVisible = false
      },
      uuid() {

        var s = [];

        var hexDigits = "0123456789abcdef";

        for (var i = 0; i < 36; i++) {

          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);

        }

        s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010

        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01

        s[8] = s[13] = s[18] = s[23] = "-";


        var uuid = s.join("");

        return uuid;

      },
      confirmDeliery() {
        this.$confirm(this.$t("Tips.AreShipmentsConfirmed"), this.$t("Tips.tips"), {
          confirmButtonText: this.$t("Tips.yes"),
          cancelButtonText: this.$t("Tips.no"),
          type: 'warning',
          center: true
        }).then(() => {
            // let amouts=[];
            // this.search_result.forEach(item=>{
            //   amouts.push(item.materialAmount);
            // })
            // activityService.excuteDeliver({idsStrT:this.multipleSelectionT,billNo:this.billNo,transportation:this.transportation,shipNo:this.shipNo,containerNo:this.containerNo,contractNo:this.contractNo,amouts:amouts,dateFlag:this.dateFlag}).then(resp=>{
            //   if (resp.data.code=="201"){
            //     this.$notify({message: resp.data.message, type: "success"});
            //     this.$emit("success");
            //     this.dialogFormVisible = false
            //   } else {
            //     this.$notify({message: resp.data.message, type: "error"});
            //   }
            // })

            // this.search_result.forEach(item=>{
            //   orderDetailInfoId.push(item.id);
            //   let objectA={
            //
            //     // materialCode:item.materialCode,
            //     // sendAmount:item.amount,
            //     // suggestedAmount:item.suggestedAmount
            //   };
            //   objectA[item.id]=item.materialAmount
            //   orderDetailInfoDeliveryAmount.push(objectA)
            // });
            let id = this.uuid().replace(/-/g,"");
            this.search_result.forEach(item => {
              item['deliveryAmount'] = item.materialAmount
            })
            let objectA = {
              id:id,
              contractNo: this.form.contractNo,
              packingDate: this.form.packingDate,
              departureDate: this.form.departureDate,
              shippingMethods: this.form.shippingMethods,
              billNo: this.form.billNo,
              containerSize: this.form.containerSize,
              containerNo: this.form.containerNo,
              freightPremium: this.form.freightPremium,
              paymentDay: this.form.paymentDay,
              paymentDate: this.form.paymentDate,
              sendAmount: this.realAmount,//实际数量
              suggestedAmount: this.allSuggest,
              materialCode: this.search_result[0].materialCode,
              materialName: this.search_result[0].materialDescCn,
              supplierName: this.search_result[0].msupplierInfoEntity && this.search_result[0].msupplierInfoEntity.supplierNameCn || "",
              unit: this.search_result[0].materialUnit,
              orderDetailInfoList: this.search_result,
            }
            activityService.excuteDeliver({...objectA}).then(resp => {
              if (resp.data.code == "201") {

                let objectArray = [];
                this.search_result.forEach(item => {
                  let objectB = {
                    uuid:id,
                    orderCode: item.orderCode,
                    materialCode: item.materialCode,
                    materialName: item.materialDescCn,
                    contractNo: this.form.contractNo,
                    packingDate: this.form.packingDate,
                    departureDate: this.form.departureDate,
                    shippingMethods: this.form.shippingMethods,
                    billNo: this.form.billNo,
                    containerSize: this.form.containerSize,
                    containerNo: this.form.containerNo,
                    supplierName: item.msupplierInfoEntity && item.msupplierInfoEntity.supplierNameCn || "",
                    unit: item.materialUnit,
                    sendAmount: item.materialAmount,
                    price: item.materialVatPrice,
                    amount: (item.materialAmount && item.materialAmount || 0) * (item.materialVatPrice && item.materialVatPrice || 0),
                    payment: 1,
                    paymentDay: this.form.paymentDay,
                    paymentDate: this.form.paymentDate,
                    freightPremium: this.form.freightPremium,
                  }
                  objectArray.push(objectB)
                });
                activityService.excuteDeliverSplit(objectArray).then(resp => {
                  if (resp.data.code == "201") {
                    this.$notify({message: resp.data.message, type: "success"});
                    this.$emit("success");
                    this.dialogFormVisible = false
                  }
                })
              } else {
                this.$notify({message: resp.data.message, type: "error"});
              }
            })
          }
        ).catch(() => {
          this.internal_activated = true;
        })
      }
    }
  }
</script>

<style scoped>

</style>
